package panel;

import information.Admin;
import information.Building;
import information.Person;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JPanel;

import screen.Main;
import file.FileManagement;
import function.Login;
import function.Menu;
import function.Parking;
import function.Statics;

@SuppressWarnings("serial")
public class Window extends JPanel{

	/*main screen*/
	Main main;
	
	/*window*/
	private int stateButtonIconified, stateButtonExit;
	
	/*login*/
	private boolean stateLogin;
	private int stateButtonLogin, stateButtonLogout;
	Person person;			//Admin or User
	
	/*menu*/
	private int stateButtonMenu[] = new int[4];
	Building building;
	
	/*mode*/
	private boolean modeStatics;
	
	/*function*/
	Parking parking;
	Statics statics;
	Login login;
	Menu menu;
	
	/*file*/
	FileManagement fm;
	
	public Window(Main main){
		this.main = main;
		
		/*Function Class*/
		parking = new Parking(this);
		statics = new Statics(this);
		login = new Login(this);
		menu = new Menu(this);
		
		/*Panel*/
		setLayout(null);
	}
	
	
	public Main getMain() {
		return main;
	}
	
	public void setStateButtonIconified(int stateButtonIconified) {
		this.stateButtonIconified = stateButtonIconified;
	}

	public void setStateButtonExit(int stateButtonExit) {
		this.stateButtonExit = stateButtonExit;
	}

	public boolean isStateLogin() {
		return stateLogin;
	}

	public void setStateLogin(boolean stateLogin) {
		if(stateLogin == false){
			person = null;
			login.getId().setText("");
			login.getPwd().setText("");
			login.getId().setVisible(true);
			login.getPwd().setVisible(true);
		}
		this.stateLogin = stateLogin;
	}
	
	public int getStateButtonLogin() {
		return stateButtonLogin;
	}

	public void setStateButtonLogin(int stateButtonLogin) {
		this.stateButtonLogin = stateButtonLogin;
	}
	
	public int getStateButtonLogout() {
		return stateButtonLogout;
	}

	public void setStateButtonLogout(int stateButtonLogout) {
		this.stateButtonLogout = stateButtonLogout;
	}

	public void createAdmin(){
		login.getId().setVisible(false);
		login.getPwd().setVisible(false);
		person = new Admin("root","1234","Admin",99,"010-1234-5678",null,0,0,false);
	}
	
	public void createUser(){
		login.getId().setVisible(false);
		login.getPwd().setVisible(false);
//		person = new User("root","1234","Admin",99,"010-1234-5678",null,0,0,false);
	}
	
	public Person getPerson(){
		return person;
	}
	
	public int getStateButtonMenu(int i) {
		if(i<0)
			return stateButtonMenu[0];
		else if(i>3)
			return stateButtonMenu[3];
		else
			return stateButtonMenu[i];
	}

	public void setStateButtonMenu(int i, int value) {
		if(i<0){
			for(int j=0;j<4;j++)
				this.stateButtonMenu[j] = 0;
			this.stateButtonMenu[0] = value;
		}else if(i>3){
			for(int j=0;j<4;j++)
				this.stateButtonMenu[j] = 0;
			this.stateButtonMenu[3] = value;
		}else{
			for(int j=0;j<4;j++)
				this.stateButtonMenu[j] = 0;
			this.stateButtonMenu[i] = value;
			
		}
	}
	
	public void createBuilding(){
	//	person = new Admin("root","1234","Admin",99,"010-1234-5678",null,0,0,false);
	}
	
	public Building getBuilding(){
		return building;
	}
	
	public boolean isModeStatics() {
		return modeStatics;
	}

	public void setModeStatics(boolean modeStatics) {
		this.modeStatics = modeStatics;
	}	
	
	public Parking getParking() {
		return parking;
	}

	public Statics getStatics() {
		return statics;
	}

	public Login getLogin() {
		return login;
	}

	public Menu getMenu() {
		return menu;
	}
	
	public FileManagement getFileManagement() {
		return fm;
	}
	
	public void paintComponent(Graphics g) {

		Toolkit winTool = this.getToolkit();
		Image buffer = createImage(800, 500);
		Graphics bufferG = buffer.getGraphics();

		/*window*/
		bufferG.drawImage(winTool.getImage(".\\Images\\Main_Screen.png"), 0, 0, this);
		bufferG.drawImage(winTool.getImage(".\\Images\\Main_Iconified"+stateButtonIconified+".png"), 711, -44, this);
		bufferG.drawImage(winTool.getImage(".\\Images\\Main_Exit"+stateButtonExit+".png"), 729, -44, this);
		
		/*Member*/
		login.draw(bufferG, winTool, this);
		/*menu*/
		menu.draw(bufferG,winTool,this);
		/* statics */
		if (modeStatics)	statics.draw(bufferG, winTool, this);
		/* parking */
		else	parking.draw(bufferG, winTool, this);

		g.drawImage(buffer, 0, 0, this);

		setOpaque(false);
		super.paintComponent(g);
	}
}
